package com.neusoft.yuejian12.yuejian_12.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.activity.MatcthDetailActivity;
import com.neusoft.yuejian12.yuejian_12.javabean.MatchBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.neusoft.yuejian12.yuejian_12.constant.Constant.MATCH_PHOTO_URL;

public class MatchListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List mDataList;

    private LayoutInflater mLayoutInflaterm;
    public MatchListAdapter(Context mContext,List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflaterm = LayoutInflater.from(mContext);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflaterm.from(parent.getContext())
                .inflate(R.layout.cardview_match, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final MatchBean entity = (MatchBean) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.matchname.setText(entity.getM_name());
        String match_pic =MATCH_PHOTO_URL+entity.getM_picture();
        Picasso.with(mContext).load(match_pic).into(viewHolder.imagematch);
        if (viewHolder.imagematch.getDrawable()==null){
            viewHolder.imagematch.setImageResource(R.drawable.ic_add);
        }
        //点击item跳转
    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putInt("m_id",entity.getM_id());
          //  Log.d("m","m_id");
            bundle.putString("m_name",entity.getM_name());
            bundle.putInt("m_price",entity.getM_price());
            bundle.putString("m_address",entity.getM_address());
            bundle.putString("m_time",entity.getM_time());
            bundle.putString("m_picture",entity.getM_picture());
            bundle.putString("m_phone",entity.getM_phone());
            Intent intent=new Intent(mContext, MatcthDetailActivity.class);
            intent.putExtras(bundle);
            mContext.startActivity(intent);
        }
    });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imagematch;
        TextView matchname;
        public ViewHolder(View itemView){
            super(itemView);
            matchname = (TextView) itemView.findViewById(R.id.match_name);
            imagematch = (ImageView) itemView.findViewById(R.id.match_pic);

        }
    }







}
